<template>
  <Table
    ref="tableRef"
    :url="URL_PAGEQUERYCOSTTEMPLATE"
    :columns="data.columns"
    :handles="data.handles"
    :toolbar="data.toolbar"
    :query="data.query"
  ></Table>
</template>

<script lang="ts" setup name="priceTemplateList">
import Table from '@/components/table/index.vue';
import {
  URL_PAGEQUERYCOSTTEMPLATE,
  delCostTemplate,
} from '@/service/apis/pile';
import { queryDicByParent } from '@/service/apis/api';

const router = useRouter();

const tableRef = ref();

const emit = defineEmits(['get-update']);
onActivated(() => {
  emit('get-update', (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

queryDicByParent({
  listDtos: ['protocolVersion'],
}).then((res: any) => {
  if (res) {
    data.columns[2].dic = [...res.protocolVersion];
    data.query[1].props.options = [...res.protocolVersion];
  }
});

const data = reactive({
  columns: [
    {
      prop: 'templateName',
      label: '模板名称',
    },
    {
      prop: 'attributionName',
      label: '归属公司',
    },
    {
      prop: 'protocolVersion',
      label: '协议版本',
      dic: [] as any,
    },
    {
      prop: 'isUsed',
      label: '是否已应用',
      template: (row: any, index: number) => {
        return row.isUsed ? '是' : '否';
      },
    },
    {
      prop: 'updateBy',
      label: '修改人',
    },
    {
      prop: 'updateTime',
      label: '修改时间',
    },
  ],
  handles: [
    {
      label: '应用电桩',
      handle: (row: any) => {
        console.log('应用电桩 ---', row);
        router.push({
          name: 'priceTemplateDetail',
          query: { id: row.id },
        });
      },
    },
    {
      label: '修改',
      permission: 'priceTemplateList.edit',
      handle: (row: any) => {
        console.log('编辑---', row);
        router.push({
          name: 'priceTemplateEdit',
          query: { id: row.id },
        });
      },
    },
    {
      label: '复制',
      permission: 'priceTemplateList.copy',
      handle: (row: any) => {
        console.log('复制---', row);
        router.push({
          name: 'priceTemplateEdit',
          query: { id: row.id, isCopy: 1 },
        });
      },
    },
    {
      label: '删除',
      color: '#FA3939',
      permission: 'priceTemplateList.delete',
      handle: (row: any) => {
        ElMessageBox.confirm('此操作将永久删除该数据，是否继续？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          delCostTemplate({
            id: row.id,
          }).then((res: any) => {
            if (res) {
              tableRef.value.getData();
            }
          });
        });
      },
    },
  ],
  toolbar: [
    {
      label: '新增',
      permission: 'priceTemplateList.add',
      icon: 'icon-add',
      type: 'primary',
      handle: (row: any) => {
        router.push({
          name: 'priceTemplateAdd',
        });
      },
    },
  ],
  query: [
    {
      label: '模板名称',
      prop: 'templateName',
      comName: 'input',
      props: {
        placeholder: '请输入模板名称',
      },
    },
    {
      label: '协议版本',
      prop: 'protocolVersion',
      comName: 'select',
      props: {
        placeholder: '请选择协议版本',
        change: (value: any) => {
          console.log(value);
        },
        options: [] as any,
      },
    },
  ],
});
</script>